package com.hua.yuan.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hua.yuan.R;
import com.hua.yuan.ui.ImageDetailActivity;
import com.hua.yuan.ui.module.ImageSubItem;
import com.hua.yuan.util.CheckLoginUtil;

import java.util.List;

/**
 * Created by huangyang on 2018/7/16.
 * 显示图集的recycleAdapter
 */

public class DetailRecycleAdapter extends RecyclerView.Adapter<DetailRecycleAdapter.HolderView> {

    private Context context;

    private List<ImageSubItem> list;

    public DetailRecycleAdapter(Context context) {
        this.context = context;
    }

    public List<ImageSubItem> getList() {
        return list;
    }

    public void setList(List<ImageSubItem> list) {
        this.list = list;
    }

    @Override
    public HolderView onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_det_item_view, null);

        return new HolderView(view);
    }

    @Override
    public void onBindViewHolder(HolderView holder, int position) {

        final ImageSubItem bean = list.get(position);

        holder.detImageName.setText(bean.getTitle());

        Glide.with(context)
                .load(bean.getCover_img_url())
                .into(holder.detImage);

        holder.detImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CheckLoginUtil.check()){
                    context.startActivity(new Intent(context, ImageDetailActivity.class).putExtra("id", bean.getAtlas_id()));
                }else {
                    CheckLoginUtil.goToLogin((Activity) context);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class HolderView extends RecyclerView.ViewHolder {


        private ImageView detImage;
        private TextView detImageName;


        HolderView(View itemView) {
            super(itemView);

            detImage = itemView.findViewById(R.id.detImage);
            detImageName = itemView.findViewById(R.id.detImageName);

        }
    }

}
